<?php
/**
 * Webtaculous_Mail
 *
 * Extends Zend_Mail adding logging to the database.
 *
 * @package default
 * @author Colin Burn-Murdoch
 **/
class Webtaculous_Mail extends Zend_Mail {
	
	protected $db;
	protected $site_id;
	protected $id;
	
	public function __construct($charset = 'UTF-8', $db, $site_id) {
		$this->db = $db;
		$this->site_id = $site_id;
		parent::__construct($charset);
	}
	
	public function send() {
		$result = parent::send();
		
		$email_log = array(
			'site_id'     => $this->site_id,
			'from'        => $this->_from,
			'to'          => join(", ", $this->_to),
			'subject'     => $this->_subject,
			'date'        => date('Y-m-d H:i:s', strtotime($this->_date)),
			'remote_addr' => $_SERVER['REMOTE_ADDR']
		);
		if ($this->_bodyHtml) {
			$email_log['html'] = quoted_printable_decode($this->_bodyHtml->getContent());
		}
		if ($this->_bodyText) {
			$email_log['text'] = quoted_printable_decode($this->_bodyText->getContent());
		}
		
		$this->db->insert('cms_email_log', $email_log);
		$this->id = $this->db->lastInsertId();
		
		return $result;
	}
	
	public function getId() {
		return $this->id;
	}
	
}
